
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local RoleFFI  = require("role.RoleFFI")
local Log = require("common.Log")
local PfLogic = require("tencentApi.PfLogic")

POWER_RANK = POWER_RANK or {}
--POWER_RANK = {index = {uuid,name,familyName, power, create = os.time()},index{uuid,name,familyName, power}}

function setRankList(human, list, index)
  list.type = RankDefine.POWER_RANK_TYPE
  list.name = POWER_RANK[index].name
  list.uuid = POWER_RANK[index].uuid
  list.power = POWER_RANK[index].power
  list.familyName = POWER_RANK[index].familyName
  if PfLogic.isBlueShow(human) then
    list.blue = POWER_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.powerlist[0] = #POWER_RANK
  for i=1, #POWER_RANK do
    if POWER_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.powerlist[i], i)
  end
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list) 
  list[0] = 0  
  if POWER_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
  end
end

function setRankInfo(db, index)
  POWER_RANK[index] = {}
  POWER_RANK[index].uuid = RoleFFI.ffi_string(db._id)
  POWER_RANK[index].power = db.zhandouli
  POWER_RANK[index].name = RoleFFI.ffi_string(db.name)
  POWER_RANK[index].familyName = RoleFFI.ffi_string(db.familyName)
  POWER_RANK[index].account = RoleFFI.ffi_string(db.account)
  POWER_RANK[index].create = os.time()
  POWER_RANK[index].blue = string.format("%d%d%d%d", db.blueVipLv, db.blueYearVip, db.blueSuperVip, db.level3366)
end

function sortFunc(a, b)
  if a.power == b.power then
    return a.create < b.create
  end
  return a.power > b.power
end

function isInRank(db)
  for i=1, #POWER_RANK do
    if POWER_RANK[i].uuid == RoleFFI.ffi_string(db._id) then
      if db.zhandouli ~= POWER_RANK[i].power then
        setRankInfo(db, i)
        table.sort(POWER_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end

function familyChange(db)
  for i=1, #POWER_RANK do
    if POWER_RANK[i].uuid == RoleFFI.ffi_string(db._id) then
      POWER_RANK[i].familyName = RoleFFI.ffi_string(db.familyName)
    end
  end
end

--外部调用，战力变更的时候调用
function powerChange(human, db)
  if not db then
    db = human.db
  end
  if db.lv < 35 then return end
  if isInRank(db) then
    return
  end
  if #POWER_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(db, #POWER_RANK + 1)
  elseif #POWER_RANK >= RankDefine.RANK_MAX_CNT and db.zhandouli > POWER_RANK[#POWER_RANK].power then
    setRankInfo(db, RankDefine.RANK_MAX_CNT)
  else
    return false
  end
  table.sort(POWER_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.POWER_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    POWER_RANK[tmp.index] = {}
    POWER_RANK[tmp.index].uuid = tmp.uuid
    POWER_RANK[tmp.index].power = tmp.power
    POWER_RANK[tmp.index].name = tmp.name
    POWER_RANK[tmp.index].familyName = tmp.familyName
    POWER_RANK[tmp.index].account = tmp.account
    POWER_RANK[tmp.index].create = tmp.create or os.time()
    POWER_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperPowerData(index)
  local db = {}
  db.type = RankDefine.POWER_RANK_TYPE
  db.index = index
  db.uuid = POWER_RANK[index].uuid
  db.power = POWER_RANK[index].power
  db.name = POWER_RANK[index].name
  db.familyName = POWER_RANK[index].familyName
  db.account = POWER_RANK[index].account
  db.create = POWER_RANK[index].create or os.time()
  db.blue = POWER_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #POWER_RANK do
    local db = wrapperPowerData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end

function writeLog()
  for i=1, #POWER_RANK do
    Log.write(Log.LOGID_OSS_POWER_RANK, POWER_RANK[i].uuid, POWER_RANK[i].account, POWER_RANK[i].name, POWER_RANK[i].power, i)
  end
end